package com.polylink.base;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import com.polylink.sys.bean.User;
import com.polylink.sys.service.IUserService;

/**
 * 
 * @author xiliang.xiao
 *	权限验证类
 */
public class UserRealm extends AuthorizingRealm{
	
	@Autowired
	private IUserService userService;

	/**
	 * 授权操作
	 */
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
//		String username = (String) getAvailablePrincipal(principals);
		String username = (String) principals.getPrimaryPrincipal();
		
		List<Map<String,Object>> list =  userService.findUserFunctionByUsername(username);
		//角色名的集合
		Set<String> roles = new HashSet<String>();
		//权限名的集合
		Set<String> permissions = new HashSet<String>();
		
		if(list!=null&&list.size()>0){
			for(Map<String,Object> map:list){
				roles.add((String) map.get("rolename"));
				permissions.addAll(Arrays.asList(map.get("code").toString().split(",")));
				if(map.get("remark")!=null&&StringUtils.isNotBlank(map.get("remark").toString())&&!permissions.contains(map.get("remark").toString())){
					permissions.add(map.get("remark").toString());
				}
				if(map.get("premark")!=null&&StringUtils.isNotBlank(map.get("premark").toString())&&!permissions.contains(map.get("premark").toString())){
					permissions.add(map.get("premark").toString());
				}
			}
		}

		SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
		
		authorizationInfo.addRoles(roles);
		authorizationInfo.addStringPermissions(permissions);
		
		return authorizationInfo;
	}

	/**
	 * 身份验证操作
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(
			AuthenticationToken token) throws AuthenticationException {
		
		String username = (String) token.getPrincipal();
		//得到密码  
//        String password = new String((char[])token.getCredentials()); 
		User user = userService.findUserByUsername(username);
		
		if(user==null){
			//木有找到用户
			throw new UnknownAccountException("没有找到该账号");
		}
//		if(password.equals(user.getPsw())){
//			//如果密码错误  
//            throw new IncorrectCredentialsException();
//		}
		//交给shiro验证器验证
		SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user.getName(), user.getPsw(),getName());
		return info;
	}
	
	@Override
	public String getName() {
		return getClass().getName();
	}

	//清空所有权限缓存
	public void clearAllCachedAuthorizationInfo() {
        getAuthorizationCache().clear();
    }

	//清空所有登录缓存
    public void clearAllCachedAuthenticationInfo() {
        getAuthenticationCache().clear();
    }
}
